﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DevComponents.DotNetBar.Controls;
using System.Data;
using System.Windows.Forms;

namespace BUL
{
   // // anh dung Partial la de anh khong phai khai bao lai bu su dung trong main lan nua.

    public partial class Business
    {
        public void LoadSourceKhuVuc(ComboBox comboBox)
        {
            // lấy danh sách Khu vực để hiện thị trong combo
            var result = _dal.tblKhuVucs;
            // Thiết lập giá trị khi lựa chọn một phân từ trong combobox là trường MaKhuVuc
            comboBox.ValueMember = "MaKhuVuc";
            // Thiết lập giá trị Hiển thị trên comboBox
            comboBox.DisplayMember = "TenKhuVuc";
            // gán Suorce cho COmboBox
            comboBox.DataSource = result;
        }

        public void GetAllNPP(string strTimKiem, DataGridViewX dataGridViewX)
        {
            
            if (dataGridViewX.Rows.Count > 0)
                dataGridViewX.Rows.Clear(); 
            var result = _dal.tblNhaPhanPhois.Include("tblKhuVucs").Where(x => x.MaNPP.Contains(strTimKiem)).OrderByDescending(x => x.TenNPP).Select(x => new { MaNPP = x.MaNPP, TenNPP = x.TenNPP, TenKV = x.tblKhuVuc.TenKhuVuc, DiaChi = x.DiaChi, DienThoai = x.DienThoai, Email = x.Email, Fax = x.Fax, SoTaiKhoan = x.SoTaiKhoan, NganHang = x.TenNganHang, MaSoThue = x.MaSoThue }).ToList();
            int i = 0;
            foreach (var item in result)
            {
                
                dataGridViewX.Rows.Add();
                dataGridViewX.Rows[i].Cells[0].Value = "N";
                dataGridViewX.Rows[i].Cells[1].Value = item.MaNPP;
                dataGridViewX.Rows[i].Cells[2].Value = item.TenNPP;
                dataGridViewX.Rows[i].Cells[3].Value = item.TenKV;
                dataGridViewX.Rows[i].Cells[4].Value = item.DiaChi;
                dataGridViewX.Rows[i].Cells[5].Value = item.DienThoai;
                dataGridViewX.Rows[i].Cells[6].Value = item.Email;
                dataGridViewX.Rows[i].Cells[7].Value = item.Fax;
                dataGridViewX.Rows[i].Cells[8].Value = item.SoTaiKhoan;
                dataGridViewX.Rows[i].Cells[9].Value = item.NganHang;
                dataGridViewX.Rows[i].Cells[10].Value = item.MaSoThue;
                i++;
            }
        }

        public tblNhaPhanPhoi GetNhaPPById(string manhapp)
        {
            return _dal.tblNhaPhanPhois.Where(x => x.MaNPP == manhapp).FirstOrDefault();
        }

        public void InsertNhaPP(tblNhaPhanPhoi nhapp)
        {
            _dal.tblNhaPhanPhois.AddObject(nhapp);
            _dal.SaveChanges();
        }

        public void UpdateNhaPP(string maNhaPPCu,tblNhaPhanPhoi nhapp)
        {
            var NPPUpdate = _dal.tblNhaPhanPhois.Where(x => x.MaNPP == maNhaPPCu).FirstOrDefault();
            NPPUpdate.MaNPP = nhapp.MaNPP;
            NPPUpdate.TenNPP = nhapp.TenNPP;
            NPPUpdate.MaKhuVuc = nhapp.MaKhuVuc;
            NPPUpdate.DiaChi = nhapp.DiaChi;
            NPPUpdate.DienThoai = nhapp.DienThoai;
            NPPUpdate.Email = nhapp.Email;
            NPPUpdate.Fax = nhapp.Fax;
            NPPUpdate.SoTaiKhoan = nhapp.SoTaiKhoan;
            NPPUpdate.TenNganHang = nhapp.TenNganHang;
            NPPUpdate.MaSoThue = nhapp.MaSoThue;
            _dal.SaveChanges();
        }

        public void DeleteNhaPP(IEnumerable<string> manhapps)
        {
            var listDelete = _dal.tblNhaPhanPhois.Where(x => manhapps.Contains(x.MaNPP));
            foreach (var tblnhapp in listDelete)
            {
                _dal.tblNhaPhanPhois.DeleteObject(tblnhapp);
            }
            _dal.SaveChanges();
        }

        public tblNhaPhanPhoi BuildNhaPP(string maNPP, string tenNPP, string maKV, string diachi, string dienthoai, string email, string fax, string sotaikhoan, string nganhang, string masothue)
        {
            return new tblNhaPhanPhoi { MaNPP = maNPP, TenNPP = tenNPP, MaKhuVuc = maKV, DiaChi = diachi, DienThoai = dienthoai, Email = email, Fax = fax, SoTaiKhoan = sotaikhoan, TenNganHang = nganhang, MaSoThue = masothue  };
        }
    }
}
